package org.litu.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import org.litu.app.constant.SysContant;
import org.litu.app.dao.SysLogsMapper;
import org.litu.app.entity.system.SysLogs;
import org.litu.app.service.ISysLogsService;
import org.litu.base.service.impl.BaseServiceImpl;
import org.litu.core.login.UserInfo;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 系统日志服务实现类
 * </p>
 *
 * @author ltgk
 * @since 2018-10-27
 */
@Service
public class SysLogsServiceImpl extends BaseServiceImpl<SysLogsMapper, SysLogs> implements ISysLogsService {

    @Override
    public void beforePage(UserInfo user, SysLogs sysLogs, String keyword, IPage<SysLogs> page, Map<String, String> params, LambdaQueryWrapper<SysLogs> query) {
        if (StringUtils.isNotBlank(params.get(SysContant.QUERY_START_TIME))) {
            query.ge(SysLogs::getCreateTime, params.get(SysContant.QUERY_START_TIME));
        }
        if (StringUtils.isNotBlank(params.get(SysContant.QUERY_END_TIME))) {
            query.le(SysLogs::getCreateTime, params.get(SysContant.QUERY_END_TIME));
        }
        if (StringUtils.isNotBlank(sysLogs.getModule())) {
            query.like(SysLogs::getModule, sysLogs.getModule());
        }
        if (StringUtils.isNotBlank(sysLogs.getOptType())) {
            query.like(SysLogs::getOptType, sysLogs.getOptType());
        }
        if (StringUtils.isNotBlank(sysLogs.getSystemCode())) {
            query.eq(SysLogs::getSystemCode, sysLogs.getSystemCode());
        }
        query.orderByDesc(SysLogs::getCreateTime);
    }

    /**
     * 根据时间段删除日志信息
     *
     * @param startTime 开始时间，日期+时间
     * @param endTime   结束时间，日期+时间
     * @return
     */
    @Override
    public boolean deleteByTime(String startTime, String endTime) {
        LambdaQueryWrapper<SysLogs> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.ge(SysLogs::getCreateTime, startTime);
        queryWrapper.le(SysLogs::getCreateTime, endTime);

        return remove(queryWrapper);
    }
}
